package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.scm.entity.ScmCloudChain;
import org.jeecg.modules.scm.entity.ScmReceivablesTransferContract;
import org.jeecg.modules.scm.service.IScmReceivablesTransferApplicationService;
import org.jeecg.modules.scm.service.IScmReceivablesTransferContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: scm_receivables_transfer_contract
 * @Author: jeecg-boot
 * @Date: 2021-11-09
 * @Version: V1.0
 */
@Api(tags = "应收账款转让合同")
@RestController
@RequestMapping("/transfer/scmReceivablesTransferContract")
@Slf4j
public class ScmReceivablesTransferContractController extends JeecgController<ScmReceivablesTransferContract, IScmReceivablesTransferContractService> {
    @Autowired
    private IScmReceivablesTransferContractService scmReceivablesTransferContractService;
    @Autowired
    private IScmReceivablesTransferApplicationService scmReceivablesTransferApplicationService;

    /**
     * 合同签约
     *
     * @param
     * @return
     */
    @AutoLog(value = "合同签约")
    @ApiOperation(value = "合同签约", notes = "合同签约")
    @PostMapping(value = "/sign")
    public Result<?> sign(@RequestParam(name = "contractId") String contractId) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Result<?> result = scmReceivablesTransferContractService.sign(contractId, sysUser.getCusNo());
        return result;
    }

    /**
     * @title
     * @description
     * @author Zhou Ruiji
     * @updateTime 2021/12/9 16:03
     * @throws
     */
    @AutoLog(value = "查询申请对应合同")
    @ApiOperation(value = "查询申请对应合同", notes = "查询申请对应合同")
    @GetMapping(value = "/findContractByApllyId")
    public Result<?> findContractByApllyId(@RequestParam(name = "apllyId") String apllyId) {
        LambdaQueryWrapper<ScmReceivablesTransferContract> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ScmReceivablesTransferContract::getTransferApplyId, apllyId);
        ScmReceivablesTransferContract contract = scmReceivablesTransferContractService.getOne(queryWrapper);
        if (oConvertUtils.isEmpty(contract)) {
            return Result.error("未查到合同信息");
        }
        return Result.OK(contract);
    }

    /**
     * 查询申请关联的云链单
     *
     * @param
     * @return
     */
    @AutoLog(value = "查询申请关联的云链单")
    @ApiOperation(value = "查询申请关联的云链单", notes = "查询申请关联的云链单")
    @PostMapping(value = "/findCloudChain")
    public Result<?> findCloudChain(@RequestParam(name = "id") String id) {
        List<ScmCloudChain> cloudChain = scmReceivablesTransferContractService.findCloudChain(id);
        if (oConvertUtils.isEmpty(cloudChain)) {
            return Result.error("没有关联云链单");
        }
        return Result.OK(cloudChain);
    }


    /**
     * 分页列表查询
     *
     * @param scmReceivablesTransferContract
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ScmReceivablesTransferContract scmReceivablesTransferContract,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ScmReceivablesTransferContract> queryWrapper = QueryGenerator.initQueryWrapper(scmReceivablesTransferContract, req.getParameterMap());
        Page<ScmReceivablesTransferContract> page = new Page<ScmReceivablesTransferContract>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (!user.getOrganization().getStatus().equals(1)) {
            queryWrapper.eq("transferor_cus_no",user.getCusNo()).or().eq("transferee_cus_no",user.getCusNo());
        }
        IPage<ScmReceivablesTransferContract> pageList = scmReceivablesTransferContractService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param scmReceivablesTransferContract
     * @return
     */
    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ScmReceivablesTransferContract scmReceivablesTransferContract) {
        scmReceivablesTransferContractService.save(scmReceivablesTransferContract);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param scmReceivablesTransferContract
     * @return
     */
    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ScmReceivablesTransferContract scmReceivablesTransferContract) {
        scmReceivablesTransferContractService.updateById(scmReceivablesTransferContract);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id删除")
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        scmReceivablesTransferContractService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.scmReceivablesTransferContractService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        ScmReceivablesTransferContract scmReceivablesTransferContract = scmReceivablesTransferContractService.getById(id);
        if (scmReceivablesTransferContract == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmReceivablesTransferContract);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmReceivablesTransferContract
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmReceivablesTransferContract scmReceivablesTransferContract) {
        return super.exportXls(request, scmReceivablesTransferContract, ScmReceivablesTransferContract.class, "scm_receivables_transfer_contract");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmReceivablesTransferContract.class);
    }

}
